import { expect } from 'chai';
import time, {
    calculateDaysBetween,
    diffDays,
    getDayOfYear,
    startOfDay,
} from '../src/time';

describe('time unit test', function () {
    it('should return empty string if no arguments is given', function () {
        expect(time.format()).to.eql('');
    });

    it('should return date for valid argument', function () {
        expect(time.format('%y/%m/%d', 1589776171)).to.eql('2020/05/18');
    });

    it('should return date time for valid argument', function () {
        expect(time.format('%y-%m-%d %h:%M:%s', 1589776171)).to.eql(
            '2020-05-18 12:29:31'
        );
    });

    it('should return date time for valid argument', function () {
        expect(time.format('%h:%M:%s', 1589776141)).to.eql('12:29:01');
    });

    it('should return true for the same day', function () {
        expect(time.isSameDay(1589731200, 1589776171)).to.be.true;
    });

    it('should return false for different days', function () {
        expect(time.isSameDay(1589731199, 1589776171)).to.be.false;
    });

    it('2000 is leap year', function () {
        expect(time.isLeap(2000)).to.be.true;
    });
    it('1900 is not leap year', function () {
        expect(time.isLeap(1900)).to.be.false;
    });
    it('2004 is leap year', function () {
        expect(time.isLeap(2004)).to.be.true;
    });

    it('should return zero', function () {
        expect(diffDays()).to.be.eql(0);
    });

    it('should return the right timestamp after 1 day', function () {
        expect(diffDays(1, 1)).to.be.eql(86401);
    });

    it('should return the start of day for 2021-03-02', function () {
        // Tue Mar 02 2021 09:30:21 GMT+0800 (中国标准时间)
        const dayTime = 1614648621;
        // Tue Mar 02 2021 00:00:00 GMT+0800 (中国标准时间)
        const startOfDayTime = 1614614400;

        expect(startOfDay(dayTime)).to.be.eql(startOfDayTime);
    });

    it('should return 0 if missing arguments', function () {
        expect(startOfDay()).to.be.eql(0);
    });

    it('should return 1 day', function () {
        const startTime = 0;
        const endTime = 24 * 3600;
        expect(calculateDaysBetween(startTime, endTime)).to.be.eql(1);
    });

    it('should return 0', function () {
        expect(calculateDaysBetween()).to.be.eql(0);
    });

    it('should return 1 for Jan 1st', function () {
        expect(getDayOfYear(new Date(2022, 0, 1))).to.be.eql(1);
    });

    it('should return 32 for Feb 1st', function () {
        expect(getDayOfYear(new Date(2022, 1, 1))).to.be.eql(32);
    });

    it('should return 365 for 2022-12-31', function () {
        expect(getDayOfYear(new Date(2022, 11, 31))).to.be.eql(365);
    });
});
